home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / RandomDot 1.1.0 / source / Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  8.3 KB  |  380 lines  |  [TEXT/KAHL]

  1. /* Utils.c - 
  2.     by David Phillip Oster October 1994 oster@netcom.com
  3.     for:
  4.     Stuart Inglis singlis@waikato.ac.nz
  5.     Department of Computer Science
  6.     University of Waikato, Hamilton, New Zealand
  7.  */
  8. #include "RandomDotMain.h"
  9. #include "Utils.h"
  10.  
  11. /* *** arithmetic functions.
  12.  */
  13. Integer Min(Integer a, Integer b){ return a < b ? a : b; }
  14.  
  15. Integer Max(Integer a, Integer b){ return a > b ? a : b; }
  16.  
  17. /* *** memory functions
  18.  */
  19.  
  20. /* HSetLockState - lock a handle, and return the previous state
  21.  */
  22. SignedByte HSetLockState(Handle h){
  23.     SignedByte    state;
  24.  
  25.     state = HGetState(h);
  26.     HLock(h);
  27.     return state;
  28. }
  29.  
  30. /* *** string functions.
  31.  */
  32.  
  33. /* StrMove - 
  34.  */
  35. void StrMove(const StringPtr src, StringPtr dest){
  36.     BlockMove(src, dest, Length(src) + 1);
  37. }
  38.  
  39. /* StrHandle - copy a string into the heap as a handle, and return it.
  40.  */
  41. StringHandle StrHandle(const StringPtr s){
  42.     StringHandle    h;
  43.  
  44.     h = NIL;
  45.     PtrToHand(s, (Handle *) &h, Length(s) + 1);
  46.     return h;
  47. }
  48.  
  49.  
  50. /* AppendChar - safe add char to string
  51.  */
  52. void AppendChar(StringPtr s, char c){
  53.     if(Length(s) < 254){
  54.         s[0] = 1 + Length(s);
  55.         s[Length(s)] = c;
  56.     }
  57. }
  58.  
  59. /* Concat - stick the tail on the string
  60.  */
  61. void Concat(StringPtr s, StringPtr tail){
  62.     Integer newLen;
  63.  
  64.     newLen = Min(255, Length(s) + Length(tail));
  65.     BlockMove((Ptr) &tail[1] ,(Ptr) &s[1+Length(s)], newLen - Length(s));
  66.     s[0] = newLen;
  67. }
  68.  
  69. /* BlockClear - clear a block of memory
  70.  */
  71. void BlockClear(void *xp, LongInt len){
  72.     char *p;
  73.  
  74.     p = (char *) xp;
  75.     while(len-- > 0){
  76.         *p++ = 0;
  77.     }
  78. }
  79.  
  80. /* EqualBlock - true if two blocks of mem are eqivalent
  81.  */
  82. Boolean EqualBlock(void * ax, void *bx, LongInt len){
  83.     char *a, *b;
  84.  
  85.     a = (char *) ax;
  86.     b = (char *) bx;
  87.     for( ; len > 0; len--, a++, b++){
  88.         if(*a != *b){
  89.             return FALSE;
  90.         }
  91.     }
  92.     return TRUE;
  93. }
  94.  
  95. /* EqualHandle - true if two handles are equivalent
  96.  */
  97. Boolean EqualHandle(Handle a, Handle b){
  98.     LongInt    len;
  99.  
  100.     len = GetHandleSize(b);
  101.     if(GetHandleSize(a) != len){
  102.         return FALSE;
  103.     }
  104.     return EqualBlock(*a, *b, len);
  105. }
  106.  
  107.  
  108. /* NullUpdate - the empty update routine.
  109.  */
  110. pascal void NullUpdate(DialogPtr dp, Integer i){
  111. }
  112.  
  113.  
  114. /* SetWDIHandle - 
  115.  */
  116. void SetWDIHandle(WindowPtr win, Integer item, Handle h){
  117.     Integer theType;
  118.     Handle theHandle;
  119.     Rect theRect;
  120.  
  121.     GetDItem(win, item, &theType, &theHandle, &theRect);
  122.     SetDItem(win, item, theType, h, &theRect);
  123. }
  124.  
  125. /* SetDIHandle - 
  126.  */
  127. void SetDIHandle(Integer item, Handle h){
  128.     SetWDIHandle(qd.thePort, item, h);
  129. }
  130.  
  131. /* GetWDIHandle - 
  132.  */
  133. Handle GetWDIHandle(WindowPtr win, Integer item){
  134.     Integer theType;
  135.     Handle theHandle;
  136.     Rect theRect;
  137.  
  138.     GetDItem(win, item, &theType, &theHandle, &theRect);
  139.     return theHandle;
  140. }
  141.  
  142. /* GetDIHandle - 
  143.  */
  144. Handle GetDIHandle(Integer item){
  145.     Integer theType;
  146.     Handle theHandle;
  147.     Rect theRect;
  148.  
  149.     GetDItem(qd.thePort, item, &theType, &theHandle, &theRect);
  150.     return theHandle;
  151. }
  152.  
  153. /* GetCIHandle - 
  154.  */
  155. ControlHandle GetCIHandle(Integer item){
  156.     Integer theType;
  157.     Handle theHandle;
  158.     Rect theRect;
  159.  
  160.     GetDItem(qd.thePort, item, &theType, &theHandle, &theRect);
  161.     return (ControlHandle) theHandle;
  162. }
  163.  
  164. /* GetDIRect - 
  165.  */
  166. void GetDIRect(Integer item, Rect *rp){
  167.     Integer theType;
  168.     Handle theHandle;
  169.  
  170.     GetDItem(qd.thePort, item, &theType, &theHandle, rp);    
  171. }
  172.  
  173. /* IsCancel - return true if Control-. or Escape
  174.  */
  175. Boolean IsCancel(EventRecord *theEvent){
  176.     if(NOT (theEvent->what == autoKey || theEvent->what == keyDown)){
  177.         return FALSE;
  178.     }
  179.     return    (theEvent->modifiers & cmdKey) ?
  180.             ((theEvent->message & charCodeMask) == '.') :
  181.             ((theEvent->message & charCodeMask) == kEscapeChar);
  182. }
  183.  
  184. /* IsReturnEnter - true is c == either return or enter
  185.  */
  186. Boolean IsReturnEnter(char c){
  187.     return (c == kReturnChar) || (c == kEnterChar);
  188. }
  189.  
  190.  
  191.  
  192. /* MirrorIt - show the button changing state
  193.  */
  194. void MirrorIt(Integer itemHit){
  195.     LongInt dontcare;
  196.  
  197.     HiliteControl((ControlHandle) GetDIHandle(itemHit), inButton);
  198.     Delay(10, &dontcare);
  199.     HiliteControl((ControlHandle) GetDIHandle(itemHit), kEnable);
  200. }
  201.  
  202. /* *** window functions.
  203.  */
  204.  
  205. /* GetContentsRect - return the contents area of the window.
  206.  */
  207. void GetContentsRect(Rect *rp){
  208.     *rp = qd.thePort->portRect;
  209.     rp->bottom -= kScrollBarWidth;
  210.     rp->right -= kScrollBarWidth;
  211. }
  212.  
  213.  
  214. /* RestrictClipRect - set clip to intersection of old and new, and return old.
  215.  */
  216. RgnHandle RestrictClipRect(const Rect *r){
  217.     RgnHandle oldClip, smallClip;
  218.  
  219.     oldClip = NewRgn();
  220.     smallClip = NewRgn();
  221.     GetClip(oldClip);
  222.     RectRgn(smallClip, r);
  223.     SectRgn(qd.thePort->clipRgn, smallClip, smallClip);
  224.     SetClip(smallClip);
  225.     DisposeRgn(smallClip);
  226.     return oldClip;
  227. }
  228.  
  229. /* RestoreClip - match a previous RestrictClipRect, and save the clip.
  230.  */
  231. void RestoreClip(RgnHandle rgn){
  232.     SetClip(rgn);
  233.     DisposeRgn(rgn);
  234. }
  235.  
  236. /* InitScrollBarClass - if this is the first time, then initialize the UPP fields.
  237.  */
  238. void InitScrollBarClass(ScrollBarClassPtr p){
  239.     if(NIL == p->up && NIL != p->upProc){
  240.         p->up = NewControlActionProc(p->upProc);
  241.     }
  242.     if(NIL == p->down && NIL != p->downProc){
  243.         p->down = NewControlActionProc(p->downProc);
  244.     }
  245.     if(NIL == p->pageUp && NIL != p->pageUpProc){
  246.         p->pageUp = NewControlActionProc(p->pageUpProc);
  247.     }
  248.     if(NIL == p->pageDown && NIL != p->pageDownProc){
  249.         p->pageDown = NewControlActionProc(p->pageDownProc);
  250.     }
  251.     if(NIL == p->thumb && NIL != p->thumbProc){
  252.         p->thumb = NewControlIndicatorProc(p->thumbProc);
  253.     }
  254. }
  255.  
  256. /* ControlProc - return the appropriate Pascal procedure
  257.     if mouse leaves part, do nothing.
  258.  */
  259.  
  260. static ControlActionUPP ControlProc(ControlHandle theControl, Integer part){
  261.     ScrollBarClassPtr p;
  262.  
  263.     if(NIL == (p = (ScrollBarClassPtr) GetCRefCon(theControl))){
  264.         return NIL;
  265.     }
  266.     switch(part){
  267.         case inUpButton:    return p->up;
  268.         case inDownButton:    return p->down;
  269.         case inPageUp:        return p->pageUp;
  270.         case inPageDown:    return p->pageDown;
  271.         case inThumb:        return (ControlActionUPP) p->thumb;
  272.     }
  273.     return NIL;
  274. }
  275.  
  276. /* TrackScroll - track a scroll bar. takes a pointer to a vector of
  277.  * procedures in the refcon of the scroll bar, and calls the appropriate
  278.  * one repeatedly, as appropriate.
  279.  */
  280. Boolean TrackScroll(Point where){
  281.     ControlHandle theControl;
  282.     Integer part, oldVal;
  283.     ScrollBarClassPtr p;
  284.  
  285.     if(0 != (part = FindControl(where, qd.thePort, &theControl)) ){
  286.         if(inThumb == part){
  287.             oldVal = GetCtlValue(theControl);
  288.             p = (ScrollBarClassPtr) GetCRefCon(theControl);
  289.             (*p->thumbInit)(theControl);
  290.         }
  291.         if(inThumb == TrackControl(theControl, where, ControlProc(theControl, part))){
  292.             p = (ScrollBarClassPtr) GetCRefCon(theControl);
  293.             (* p->thumbDone)(theControl, oldVal);
  294.         }
  295.         return TRUE;
  296.     }
  297.     return FALSE;
  298. }
  299.  
  300. /* TitleBarOnScreen - return TRUE if the title bar that has rp for an
  301.     area has some pixels that are visible.
  302.  */
  303. Boolean TitleBarOnScreen(const Rect *rp){
  304.     Rect        r;
  305.     RgnHandle    rgn;
  306.     Boolean        val;
  307.  
  308.     r.left = rp->left;
  309.     r.top = r.bottom - 20;
  310.     r.right = rp->right;
  311.     r.bottom = rp->top;
  312.     InsetRect(&r, 4, 4);
  313.     rgn = NewRgn();
  314.     RectRgn(rgn, &r);
  315.     SectRgn(rgn, LMGetGrayRgn(), rgn);
  316.     val = NOT EmptyRgn(rgn);
  317.     DisposeRgn(rgn);
  318.     return val;
  319. }
  320.  
  321.  
  322. /* *** File functions
  323.  */
  324.  
  325. /* SavePreferencesResource - copy a handle into the preferences file.
  326.     (argument is unchanged)
  327.  */
  328. void SavePreferencesResource(Handle h, OSType type, Integer id){
  329.     Integer    saveRes;
  330.     Handle    saveH;
  331.     OSErr    errCode;
  332.  
  333.     if(-1 != prefResFile){
  334.         saveRes = CurResFile();
  335.         UseResFile(prefResFile);
  336.         if(NIL == (saveH = Get1Resource(type, id))){
  337.             saveH = NewHandle(GetHandleSize(h));
  338.             AddResource(saveH, type, id, "\p");
  339.         }
  340.         if(NOT EqualHandle(h, saveH)){
  341.             HNoPurge(saveH);
  342.             SetHandleSize(saveH, GetHandleSize(h));
  343.             if(noErr == (errCode = MemError())){ BlockMove(*h, *saveH, Min(GetHandleSize(h), GetHandleSize(saveH))); }
  344.             if(noErr == errCode){ ChangedResource(saveH); errCode = ResError(); }
  345.             if(noErr == errCode){ WriteResource(saveH); errCode = ResError(); }
  346.             HPurge(saveH);
  347.         }
  348.         UseResFile(saveRes);
  349.     }
  350. }
  351.  
  352. /* GetPreferencesHandle - get a handle from the prefrences file.
  353.     returns NIL on not found. Note: this is a handle, not a resource.
  354.     the caller must Dispose it.
  355.  */
  356. Handle GetPreferencesHandle(OSType type, Integer id){
  357.     Integer    saveRes;
  358.     Handle    h;
  359.  
  360.     if(-1 == prefResFile){
  361.         return NIL;
  362.     }
  363.     saveRes = CurResFile();
  364.     UseResFile(prefResFile);
  365.     if(NIL != (h = Get1Resource(type, id))){
  366.         DetachResource(h);
  367.         HNoPurge(h);
  368.     }
  369.     UseResFile(saveRes);
  370.     return h;
  371. }
  372.  
  373. /* EqualFSSpec - return TRUE if two file specs are equivalent. 
  374.  */
  375. Boolean EqualFSSpec(FSSpecPtr a, FSSpecPtr b){
  376.     return a->vRefNum == b->vRefNum && a->parID == b->parID && 
  377.         EqualString(a->name, b->name, FALSE, TRUE);
  378. }
  379.  
  380.